<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Category_management extends MX_Controller {

	public function __construct() {
		parent::__construct();

		$this->load->model('admin_category_model');
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		$this->template->set_partial('side_menu','layouts/side_menu');
		$this->template->set_partial('nav_bar','layouts/nav_bar');

	}

	public function index(){

		//$child= $this->uri->segment(4);
		//print $child;
		//die();
		//$data['category']['all']= $this->admin_category_model->get_category($child=$child);

		//print_r($data['category']['all']);
		//die();
		//$data['root']['category']=$this->admin_category_model->get_root();

		$data['parent'] = $this->admin_category_model->get_parent();


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('category/parent_category1',$data);
	}

	function sub_category($parent_id)
	{
		$parent_id = $parent_id;

		$data['child'] = $this->admin_category_model->get_subcategory($parent_id);



		//print_r($data['category']['child']);
		// 		/die();
		$data['parent_id'] = $parent_id;
		$data['child_id'] = $parent_id;

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('category/sub_category', $data);

	}

	function keywords($parent_id,$child_id)
	{
		
		//$parent_id = $parent_id;
		//$child_id = $child_id;
		$data['keyword'] = $this->admin_category_model->get_keywords($parent_id=$parent_id, $child_id=$child_id);
		//var_dump($data['keyword']);
		//die();


		$data['parent_id'] = $parent_id;
		$data['child_id'] = $child_id;
		$data['sic_code'] = $child_id;

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('category/keywords', $data);

	}






	function edit_parent_category($parent_id)
	{


		$data['parent']['category'] = $this->admin_category_model->get_parent_category($parent_id);


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')

		->build('category/edit_parent_category',$data);


	}

	function category_update()
	{

		$this->form_validation->set_rules('parent_category', 'parent_category', 'required|xss_clean');
		$this->form_validation->set_rules('parent_id', 'parent_id', 'required|xss_clean');

		$parent_id = $this->input->post('parent_id');

		if ($this->form_validation->run() == true) {


			$data = array(

					'parent_id' => $this->input->post('parent_id'),
					'name' => $this->input->post('parent_category'),

			);

			//var_dump($data);
			//die();
			//$this->db->where('id', $id);
			$this->db->where('parent_id', $parent_id);
			$this->db->where('child_id',0);
			$this->db->where('sic_code',0);

			$this->db->update('category',$data);
		}
		$this->index();
	}

	function edit_child_category($parent_id,$child_id)
	{

		$parent_id = $parent_id;
		$child_id = $child_id;

		$data['child']['category'] = $this->admin_category_model->get_child_category($parent_id=$parent_id, $child_id= $child_id);

		//var_dump($data['child']['category']);
		//die();


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')

		->build('category/edit_sub_category',$data);

	}

	function update_sub_category()
	{


		$this->form_validation->set_rules('child_category', 'child_category', 'required|xss_clean');
		$this->form_validation->set_rules('parent_id', 'parent_id', 'required|xss_clean');
		$this->form_validation->set_rules('child_id', 'child_id', 'required|xss_clean');

		$parent_id = $this->input->post('parent_id');
		$child_id = $this->input->post('child_id');

		if ($this->form_validation->run() == true) {


			$data = array(

					'parent_id' => $this->input->post('parent_id'),
					'child_id' => $this->input->post('child_id'),
					'name' => $this->input->post('child_category'),

			);
			//var_dump($data);
			//die();

			$this->db->where('parent_id', $parent_id);
			$this->db->where('child_id', $child_id);
			$this->db->where('sic_code',0);

			$this->db->update('category',$data);
		}
		redirect('secure/category/category_management/sub_category/' . $parent_id);


	}


	function edit_keyword($parent_id, $child_id, $sic_code)
	{

		$parent_id = $parent_id;
		$child_id = $child_id;
		$sic_code = $sic_code;

		$data['keyword']['list'] = $this->admin_category_model->get_keyword($parent_id=$parent_id, $child_id=$child_id, $sic_code=$sic_code);

		//var_dump($data['keyword']['list']);
		//die();


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')

		->build('category/edit_keyword',$data);
	}

	function update_keyword()
	{

		$this->form_validation->set_rules('keyword', 'keyword', 'required|xss_clean');
		$this->form_validation->set_rules('parent_id', 'parent_id', 'required|xss_clean');
		$this->form_validation->set_rules('child_id', 'child_id', 'required|xss_clean');
		$this->form_validation->set_rules('sic_code','sic_code','required|xss_clean');

		$parent_id = $this->input->post('parent_id');
		$child_id = $this->input->post('child_id');
		$sic_code = $this->input->post('sic_code');


		if ($this->form_validation->run() == true) {


			$data = array(

					'parent_id' => $this->input->post('parent_id'),
					'child_id' => $this->input->post('child_id'),
					'sic_code' => $this->input->post('sic_code'),
					'name' => $this->input->post('keyword'),

			);

			$this->db->where('parent_id', $parent_id);
			$this->db->where('child_id', $child_id);
			$this->db->where('sic_code',$sic_code);

			$this->db->update('category',$data);

		}
		redirect('secure/category/category_management/keywords/' . $parent_id. '/' .$child_id);

	}

	function add_parent()
	{
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')

		->build('category/add_parent_category');

	}

	function insert_parent()
	{

		if ($this->input->post('insert')) {
				
			$this->form_validation->set_rules('category','category','required|xss_clean|is_unique[category.name]');
				
			if($this->form_validation->run()){
					
				$data = array(
						'parent_id' =>$this->admin_category_model->get_max_parent_id(),
						'name' => $this->input->post('category'),
						);
				//var_dump($data);
				//die();
				$this->db->insert('category', $data);
				redirect('secure/category/category_management');
			}
			else
				$this->session->set_flashdata('message', 'Type a Category Name');
			redirect('secure/category/category_management/add_parent');
			} else {
				$this->session->set_flashdata('message', 'Category already exists');
				redirect('secure/category/category_management/add_parent');
			}
		}

	function add_child($parent_id)
	{
		
		
		$parent_id = $parent_id;
		
		$data['parent_id'] = $parent_id;
		$data['child_id'] = $parent_id;
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		
		->build('category/add_sub_category',$data);
	}
		
	function insert_child()
	{
	if ($this->input->post('insert')) {
				
			$this->form_validation->set_rules('sub_category','sub_category','required|xss_clean|is_unique[category.name]');
			$this->form_validation->set_rules('parent_id','parent_id','required|xss_clean');
						
			$parent_id = $this->input->post('parent_id');
			
			
				
			if($this->form_validation->run()){
					
				$data = array(
						'parent_id' =>$parent_id,
						'child_id' =>$this->admin_category_model->get_max_child_id(),
						'name' => $this->input->post('sub_category'),
						);
				//var_dump($data);
				//die();
				$this->db->insert('category', $data);
				redirect('secure/category/category_management/sub_category/'. $parent_id);
			}
			else
				$this->session->set_flashdata('message', 'Type a Category Name');
			redirect('secure/category/category_management/add_child/'. $parent_id);
			} else {
				$this->session->set_flashdata('message', 'Category already exists');
				redirect('secure/category/category_management/add_child/'. $parent_id);
			}
		
		
	}
	
	function add_keyword($parent_id,$child_id)
	{
		
		$parent_id = $parent_id;
		
		$data['parent_id'] = $parent_id;
		$data['child_id'] = $child_id;
		$data['sic_code'] = $child_id;
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		
		->build('category/add_keyword',$data);
		
		
	}
	function insert_keyword()
	{
		if ($this->input->post('insert')) {
	
			$this->form_validation->set_rules('keyword','keyword','required|xss_clean|is_unique[category.name]');
			$this->form_validation->set_rules('parent_id','parent_id','required|xss_clean');
			$this->form_validation->set_rules('child_id','child_id','required|xss_clean');
			
			$parent_id = $this->input->post('parent_id');
			$child_id = $this->input->post('child_id');
				
				
	
			if($this->form_validation->run()){
					
				$data = array(
						'parent_id' =>$parent_id,
						'child_id' =>$child_id,
						'sic_code' =>$this->admin_category_model->get_max_keyword(),
						'name' => $this->input->post('keyword'),
				);
				//var_dump($data);
				//die();
				$this->db->insert('category', $data);
				redirect('secure/category/category_management/keywords/'. $parent_id. '/' .$child_id);
			}
			else{
				$this->session->set_flashdata('message', 'Type a Keyword Name');
			redirect('secure/category/category_management/add_keyword/'. $parent_id. '/' .$child_id);
		} 
		}else {
			$this->session->set_flashdata('message', 'Keyword already exists');
			redirect('secure/category/category_management/add_keyword/'. $parent_id. '/' .$child_id);
		}
	
	
	}
	
	
	
	function add(){
		if($this->input->post('add_category_submit')){
			$this->form_validation->set_rules('parent_id', 'parent_id', 'required|xss_clean');
			$this->form_validation->set_rules('name', 'name', 'required|xss_clean');

			if ($this->form_validation->run()) {

				if($this->input->post('url') == ''){
					$url= strtolower(url_title($this->input->post('url')));
				}else{
					$url= strtolower(url_title($this->input->post('url')));
				}


				$post_data=array(
						'parent_id'			=>	intval($this->input->post('parent_id')),
						'name'				=>	$this->input->post('name'),
						'meta_title'		=>	$this->input->post('meta_title'),
						'meta_keyword'		=>	$this->input->post('meta_keyword'),
						'meta_description'	=>	$this->input->post('meta_description'),
						'url'				=>	$url
				);

				//-- Save to Database --//
				if($this->admin_category_model->valid_category(intval($this->input->post('parent_id')),trim($this->input->post('name'))) == TRUE){
					$this->mahana_hierarchy->insert($post_data);
					$this->session->set_flashdata('message','Category Added Successfully....!');
					redirect(config_item('admin_path').'category');
				}else{
					$this->session->set_flashdata('message','Invalid !Category exist....!');
					redirect(config_item('admin_path').'category');
				}

			}else{
				$this->session->set_flashdata('message','Invalid Input....!');
				redirect(config_item('admin_path').'category');
			}
		}
	}


}
/* End of file*/
